package com.vanysoft.framework.config.init;

import com.vanysoft.business.sys.entity.Api;
import com.vanysoft.business.sys.service.ApiService;
import com.vanysoft.framework.redis.RedisConstants;
import com.vanysoft.framework.redis.RedisService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类名称：ApplicationStartupListener
 * 类描述：系统加载完成后监听事件
 * 创建人：SharpClaw_x
 * 创建时间：2017年5月25日 下午5:18:13
 * 修改人：SharpClaw_x
 * 修改时间：2017年5月25日 下午5:18:13
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@Component

public class ApplicationStartupListener implements ApplicationListener<ContextRefreshedEvent> {
    private final Log log = LogFactory.getLog(getClass());

    private final RedisService redisService;
    private final ApiService apiService;

    public ApplicationStartupListener(RedisService redisService, ApiService apiService) {
        this.redisService = redisService;
        this.apiService = apiService;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("-------------------开始缓存Resource到redis中-------------------");
        List<Api> apis = apiService.selectAll();
        redisService.deleteRedis(RedisConstants.API_LIST);
        redisService.set(RedisConstants.API_LIST, apis);
        log.info("-------------------缓存Resource到redis完成-------------------");
    }
}
